// AniMolView.cpp : implementation of the CAniMolView class
//

#include "stdafx.h"
#include "AniMol.h"

#include "AniMolDoc.h"
#include "AniMolView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAniMolView

IMPLEMENT_DYNCREATE(CAniMolView, CScrollView)

BEGIN_MESSAGE_MAP(CAniMolView, CScrollView)
	//{{AFX_MSG_MAP(CAniMolView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAniMolView construction/destruction

CAniMolView::CAniMolView()
{
	// TODO: add construction code here

}

CAniMolView::~CAniMolView()
{
}

BOOL CAniMolView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAniMolView drawing

void CAniMolView::OnDraw(CDC* pDC)
{
	CAniMolDoc* pDoc = GetDocument();
	list<Atom>::iterator lit;
	list<Bond>::iterator bit;

	int no_of_atoms = pDoc->AtomList.size();
	int no_of_bonds = pDoc->BondList.size();
	/*lit = pDoc->AtomList.begin();
	pDC->SetTextAlign(TA_CENTER);
	pDC->SetTextAlign(TA_BOTTOM);
	for (int i = 0; i< no_of_atoms; i++)
	{
		char buff[2];
		buff[0] = lit->atom_name;
		buff[1] = '\0';
		
		if(lit->atom_name != 'C')
			pDC->TextOut(lit->x_coord*100+Xoff,lit->y_coord*100+Yoff,buff,1);
		lit++;
	}*/
	bit = pDoc->BondList.begin();
	int i=0,j=0;
	for(i=0;i<no_of_bonds;i++)
	{
		int k = 0;
		int temp1 , temp2, type;
		temp1 = bit->atom1;
		temp2 = bit->atom2;
		type = bit->bond_type;
		int tx1,ty1,tx2,ty2;
		
		lit = pDoc->AtomList.begin();
		for(j=1;j<temp1;j++)
			lit++;
		tx1 = lit->x_coord*100+Xoff;
		ty1 = lit->y_coord*100+Yoff;
		//pDC->MoveTo(lit->x_coord*100+Xoff-5*k,lit->y_coord*100+Yoff-5*k);

		lit = pDoc->AtomList.begin();
		for(j=1;j<temp2;j++)
			lit++;
		//int delta = 1; ///just an offset in case of tx1 = tx2
		tx2 = lit->x_coord*100+Xoff;
		ty2 = lit->y_coord*100+Yoff;

		bool flag1 = false;
		bool flag2 = false;
		bool flag3 = false;
		bool flag4 = false;

		if(tx1 == tx2)
		{
			tx2++;
			flag1 = true;
		}
		if(ty1 == ty2)
		{
			flag2 = true;
		}
		double slope = (ty1-ty2)/(tx1-tx2);
		
		if(slope > 0)
		{
			flag3 = true;
		}
		else
		{
			flag4 = true;
		}
		//pDC->LineTo(lit->x_coord*100+Xoff-5*k,lit->y_coord*100+Yoff-5*k);
		for(k=0; k<type; k++)
		{
			if(flag1)
			{
				pDC->MoveTo(tx1+5*k,ty1);
				pDC->LineTo(tx2+5*k,ty2);
			}
			else if(flag2)
			{
				pDC->MoveTo(tx1,ty1+5*k);
				pDC->LineTo(tx2,ty2+5*k);
			}
			else if(flag3)
			{
				pDC->MoveTo(tx1+5*k,ty1);
				pDC->LineTo(tx2+5*k,ty2);
			}
			else if(flag4)
			{
				pDC->MoveTo(tx1,ty1+5*k);
				pDC->LineTo(tx2,ty2+5*k);
			}

		}
		
		bit++;

	}


	lit = pDoc->AtomList.begin();
	pDC->SetTextAlign(TA_CENTER);
	pDC->SetTextAlign(TA_BOTTOM);
	for (i = 0; i< no_of_atoms; i++)
	{
		char buff[2];
		buff[0] = lit->atom_name;
		buff[1] = '\0';
		
		if(lit->atom_name != 'C')
			pDC->TextOut(lit->x_coord*100+Xoff,lit->y_coord*100+Yoff,buff,1);
		lit++;
	}

	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CAniMolView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CAniMolDoc* pDoc = GetDocument();
	list<Atom>::iterator lit;
	lit = pDoc->AtomList.begin();
	double minX=10000.0,minY=10000.0;
	double maxX=-10000.0,maxY=-10000.0;
	for(lit = pDoc->AtomList.begin();lit != pDoc->AtomList.end();lit++)
	{
		if(lit->x_coord > maxX)
			maxX = lit->x_coord;
		if(lit->y_coord > maxY)
			maxY = lit->y_coord;
		if(lit->x_coord < minX)
			minX = lit->x_coord;
		if(lit->y_coord < minY)
			minY = lit->y_coord;
	}
	CSize sizeTotal;
	this->Xoff = -minX*110 + 50;
	this->Yoff = -minY*110 + 50;
	// TODO: calculate the total size of this view
	//sizeTotal.cx = sizeTotal.cy = 1000;
	sizeTotal.cx = (maxX - minX)*110 + 100;
	sizeTotal.cy = (maxY - minY)*110 + 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CAniMolView diagnostics

#ifdef _DEBUG
void CAniMolView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CAniMolView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CAniMolDoc* CAniMolView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAniMolDoc)));
	return (CAniMolDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAniMolView message handlers
